package com.ztc.crawler;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ztc.crawler.DTO.base.CtripBaseDTO;
import com.ztc.crawler.DTO.base.SSLClient;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;

public final class CripBusCrawler {
    public static JSONObject start(CtripBaseDTO ctripBase, String  url){
        String charset = "utf-8";
        String result = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            HttpClient httpClient = new SSLClient();
            HttpPost httpPost = new HttpPost(url);
            //1398184502 ticket
            httpPost.setHeader("Cookie", "cticket=CFE49173DD6EE66B894934401E9AFB579DA511B37124B02BA452C19665CE79A6; isNonUser=false; _n_cid=12001083210057723062; MKT_Pagesource=H5; _RDG=28f266ac0917a92b973ae3c65620a81bb6; _RF1=223.104.25.232; _RGUID=9d2619dc-e36c-41b2-aa9f-26e1394fd591; _RSG=o64RZpT6cq9HMzJv4k9VgA; _bfa=1.1506417774409.wb0aon.1.1506417774409.1511229609942.15.171.476001; page_time=1509953621221%2C1509954875122%2C1509954887431%2C1509961188451%2C1510043336423%2C1510043814695%2C1510043823047%2C1510043824666%2C1510118249559%2C1510118255762%2C1510128755560%2C1510278529239%2C1510557477713%2C1510557572714%2C1510559243130%2C1510559286993%2C1510559332021%2C1510559338155%2C1510559344331%2C1510559352664%2C1510559367943%2C1510559370828%2C1510559468480%2C1510559603591%2C1511229609852; _jzqco=%7C%7C%7C%7C1510118250309%7C1.1057024563.1506417912806.1510118255714.1510128755283.1510118255714.1510128755283.0.0.0.14.14; _abtest_userid=ce1ee98f-5fe0-4c3f-82a3-8ab2202d7917");
            httpPost.setHeader("Host", "m.ctrip.com");
            httpPost.setHeader("Accept", "*/*");
            httpPost.setHeader("Accept-Language", "zh-cn");
            httpPost.setHeader("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/11B508_eb64__Ctrip_CtripWireless_7.8.0_CtripAPP_iOS_soa");
            StringEntity entity = new StringEntity(objectMapper.writeValueAsString(ctripBase), charset);//解决中文乱码问题
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, charset);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        JSONObject jsonObject = JSON.parseObject(result);
        return jsonObject;
    }
}
